package net.qhzw.geologr5.gis.service.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.geologr5.gis.common.framework.utils.JsonResult;
import net.qhzw.geologr5.gis.common.framework.utils.StringUtils;
import net.qhzw.geologr5.gis.service.system.entity.ProjectData;
import net.qhzw.geologr5.gis.service.system.mapper.ProjectDataMapper;
import net.qhzw.geologr5.gis.service.system.query.ProjectDataQuery;
import net.qhzw.geologr5.gis.service.system.service.ProjectDataService;
import net.qhzw.geologr5.gis.service.system.vo.project.ProjectDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ProjectDataServiceImpl implements ProjectDataService {

    @Autowired
    private ProjectDataMapper projectDataMapper;

    @Value("${file.projectDate.accsPathPrfx}")
    private String accsPathPrfx;

    /***
     * 项目资料查看失败
     * @param query
     * @return
     */
    @Override
    public JsonResult select(ProjectDataQuery query) {
        //PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> result = new HashMap<>();
        try {
            if (query.getProjectId() == null || query.getProjectId() <= 0) {
                return JsonResult.error("参数为空");
            }
            List<ProjectData> projectDataList = projectDataMapper.selectByPrijectId(query.getProjectId());
            if (projectDataList != null && !projectDataList.isEmpty()) {
                for (ProjectData projectData : projectDataList) {
                    if (projectData.getFileUrl() != null && !StringUtils.isEmpty(projectData.getFileUrl())) {
                        projectData.setFileUrl(accsPathPrfx + projectData.getFileUrl());
                    }
                }
            }
            //PageInfo<ProjectData> pageInfo = new PageInfo<>(projectDataList);
            result.put("total", projectDataList.size());
            result.put("list", projectDataList);
            return JsonResult.success(result);
        } catch (Exception e) {
            log.error("项目资料查看失败==ProjectDataServiceImpl==mthod:select==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }

    }


    /***
     * 项目资料查询
     * @param query
     * @return
     */
    @Override
    public JsonResult projectInfo(ProjectDataQuery query) {
        Map<String, Object> result = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<ProjectDataVo> projectDataList = projectDataMapper.projectInfo(query);
        if (projectDataList != null && !projectDataList.isEmpty()) {
            for (ProjectDataVo projectDataVo : projectDataList) {
                if (projectDataVo.getFileUrl() != null && !StringUtils.isEmpty(projectDataVo.getFileUrl())) {
                    projectDataVo.setFileUrl(accsPathPrfx + projectDataVo.getFileUrl());
                }
            }
        }
        PageInfo<ProjectDataVo> pageInfo = new PageInfo<>(projectDataList);
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return JsonResult.success(result);
    }


    /***
     * 编辑项目资料
     * @param query
     * @return
     */
    @Override
    public JsonResult edit(ProjectDataQuery query) {
        Integer id = query.getId();
        if (id == null || id <= 0) {
            return JsonResult.error("id为空");
        }
        ProjectData projectData = projectDataMapper.selectById(id);
        if (query.getAuthorityId() != null && query.getAuthorityId() > 0) {
            projectData.setAuthority(query.getAuthorityId());
        }

        if (!StringUtils.isEmpty(query.getFileName())) {
            projectData.setFileAlias(query.getFileName());
        }

        if (query.getIdentity() != null && query.getIdentity() > 0) {
            projectData.setIdentity(query.getIdentity());
        }

        int row = projectDataMapper.updateById(projectData);
        return result(row, "修改失败");
    }


    /***
     * 批量修改数据权限
     * @param query
     * @return
     */
    @Override
    public JsonResult btchEditAuthority(ProjectDataQuery query) {
        String ids = query.getIds();
        Integer authorityId = query.getAuthorityId();
        String[] split = ids.split(",");
        List<Integer> idlist = new ArrayList<>();
        for (String s : split) {
            idlist.add(Integer.valueOf(s));
        }
        int row = projectDataMapper.btchEditAuthority(idlist, authorityId, query.getIdentity());
        return result(row, "修改失败");
    }


    /***
     * 返回数据
     * @param row
     * @param message
     * @return
     */
    private JsonResult result(int row, String message) {
        if (row > 0) {
            return JsonResult.success();
        }
        return JsonResult.error(message);
    }
}
